package com.lemon.tuvi2015.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.tuvi2015.R;
import com.lemon.tuvi2015.object.GlobalObject;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class ZodiacAdapter extends BaseAdapter {

	Context context;
	ArrayList<GlobalObject> listZodiac;

	public ZodiacAdapter(Context context, ArrayList<GlobalObject> listZodiac) {
		this.context = context;
		this.listZodiac = listZodiac;
	}

	@Override
	public int getCount() {
		return listZodiac.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.congiap_item_grid, null);
			// holder.button = (Button)
			// convertView.findViewById(R.id.congiap_item);
			holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.congiap_item);
			holder.progressBar = (ProgressBar) convertView.findViewById(R.id.congiap_item_loading_progress);
			holder.imageView = (ImageView) convertView.findViewById(R.id.congiap_item_imageView);
			holder.textView = (TextView) convertView.findViewById(R.id.congiap_item_text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final GlobalObject zodiacObject = listZodiac.get(position);
//		holder.relativeLayout.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
////				BaseEngine.startActivityObject((ZodiacActivity)context, zodiacObject);
//				Intent intent = new Intent(context, ZodiacDetailsActivity.class);
//				intent.putExtra(TagName.KEY, zodiacObject);
//				context.startActivity(intent);
//			}
//		});
		// holder.imageView.setImageResource(zodiacObject.getIMAGEres());
		Picasso.with(context).load(zodiacObject.getIMAGE()).into(holder.imageView, new Callback() {
			@Override
			public void onSuccess() {
				holder.progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onError() {
			}
		});
		holder.textView.setText(zodiacObject.getNAME());

		// final Drawable drawableTop =
		// context.getResources().getDrawable(zodiacObject.getIMAGEres());
		// holder.button.setCompoundDrawablesWithIntrinsicBounds(null,
		// drawableTop , null, null);
		// holder.button.setText(zodiacObject.getNAME());
		// holder.button.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// Toast.makeText(context, zodiacObject.getNAME(),
		// Toast.LENGTH_SHORT).show();
		// // SingerObject singerObject=list.get(position);
		// //
		// ((MainActivity)context).switchContent(ArtistDetailFragment.class.getName(),
		// singerObject);
		// }
		// });
		return convertView;
	}

	private class ViewHolder {
		RelativeLayout relativeLayout;
		ImageView imageView;
		TextView textView;
		ProgressBar progressBar;
		// Button button;
	}
}
